function b = arrayToBinary(data, verbose)
% ARRAYTOBINARY  convert string to 0-1 sequence.
% B = ARRAYTOBINARY(DATA) converts string in DATA into (1xM) 0-1
% sequence. Each element in DATA is seen as a char, and only low 8 bits
% are converted into 8 bits. Besides, it generates a 16-bit length
% field at the head of the 0-1 sequence.
%
% The syntax of the return value B is:
% LLLLLLLL LLLLLLLL AAAAAAAA BBBBBBBB CCCCCCCC ...
% where L is the length field, A B C ... stands for the data.
%
if nargin < 2
    verbose = 1;
end

[M,N] = size(data);
datasize = M*N;
newsize = datasize * 8 + 16;
b = zeros(1, newsize);

for i=1:M
    for k=1:N
        value = uint8(data(i,k));
        for j=1:8
            index = 8*((i-1)*M+k)+j+8;
            b(1, index) = bitget(value,9-j);
        end
    end
end
for j=1:16
    b(1, j) = bitget(datasize,17-j);
end
if verbose
    fprintf(1, 'message : %dB(%db)',datasize, newsize);
end

end
